package com.sjtu.rest;

import com.sjtu.config.AdminHandlerFilter;
import com.sjtu.config.Operator;
import com.sjtu.exception.RoleNotEnoughException;
import com.sjtu.feign.TaskInfoFeign;
import com.sjtu.feign.UserAccountFeign;
import com.sjtu.web.BaseRest;
import com.sjtu.web.Result;
import com.sjtu.web.form.TaskEnableForm;
import com.sjtu.web.form.TaskInfoForm;
import com.sjtu.web.search.TaskInfoSearch;
import com.sjtu.web.vo.TaskInfoVo;
import com.sjtu.web.vo.UserAccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 任务信息相关
 * nerine
 * 1/29/2020
 */
@RequestMapping("/admin/task-info")
@RestController
public class TaskInfoRest extends BaseRest {
    @Autowired
    private UserAccountFeign userAccountFeign;
    @Autowired
    private TaskInfoFeign taskInfoFeign;

    @PostMapping("/list")
    public Result<?> list(@RequestHeader(AdminHandlerFilter.OPERATOR_ID) String operatorId,
                          @Valid @RequestBody TaskInfoSearch search) {
        UserAccountVo vo = userAccountFeign.findByUserId(operatorId).getData();
        if(vo.isNotAdmin()) {
            search.setSystemId(vo.getClientInfo().getSystemId());
        }
        return taskInfoFeign.list(search);
    }

    @GetMapping("/detail/{id}")
    public Result<TaskInfoVo> detail(@RequestHeader(AdminHandlerFilter.OPERATOR_ID) String operatorId,
                            @PathVariable String id) {
        TaskInfoVo taskInfoVo = taskInfoFeign.detail(id).getData();
        if(taskInfoVo == null) {
            return success();
        }
        UserAccountVo userAccountVo = userAccountFeign.findByUserId(operatorId).getData();
        if(userAccountVo.isNotAdmin()) {
            if(!userAccountVo.getClientInfo().equals(taskInfoVo.getClientInfo())) {
                illegalAccess();
            }
        }
        return success(taskInfoVo);
    }

    @Operator
    @PostMapping("/save")
    public Result<?> save(@RequestHeader(AdminHandlerFilter.OPERATOR_ID) String operatorId,
                          @Valid @RequestBody TaskInfoForm form) {
        UserAccountVo vo = userAccountFeign.findByUserId(operatorId).getData();
        form.setSystemId(vo.getClientInfo().getSystemId());
        form.setOperatorId(operatorId);
        if(form.getIdTaskInfo() != null) {
            TaskInfoVo taskInfoVo = taskInfoFeign.detail(form.getIdTaskInfo()).getData();
            if(!vo.getClientInfo().equals(taskInfoVo.getClientInfo())) {
                illegalAccess();
            }
        }
        return taskInfoFeign.save(form);
    }
    @Operator
    @PostMapping("/enable")
    public Result<?> enable(@RequestHeader(AdminHandlerFilter.OPERATOR_ID) String operatorId,
                            @Valid @RequestBody TaskEnableForm form) {
        form.setOperatorId(operatorId);
        UserAccountVo vo = userAccountFeign.findByUserId(operatorId).getData();
        TaskInfoVo taskInfoVo = taskInfoFeign.detail(form.getIdTaskInfo()).getData();
        if(!vo.getClientInfo().equals(taskInfoVo.getClientInfo())) {
            illegalAccess();
        }
        return taskInfoFeign.enable(form);
    }

    private void illegalAccess() {
        throw new RoleNotEnoughException("非法访问非所属子系统的数据");
    }
}
